package com.walkline.screen;

import java.util.Hashtable;

import net.rim.device.api.system.Application;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import com.walkline.util.Enumerations.RefreshAction;
import com.walkline.util.ImageItem;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.Comment;
import com.walkline.weibo.inf.CommentWrapper;
import com.walkline.weibo.inf.Favorite;
import com.walkline.weibo.inf.FavoriteWrapper;
import com.walkline.weibo.inf.Remind;
import com.walkline.weibo.inf.RepostWrapper;
import com.walkline.weibo.inf.Status;
import com.walkline.weibo.inf.StatusWrapper;
import com.walkline.weibo.inf.TagsWrapper;
import com.walkline.weibo.inf.User;
import com.walkline.weibo.inf.UserWrapper;

public class RefreshContentsScreen extends PopupScreen
{
	private Thread thread = null;
	private WeiboSDK _weibo;
	private String _param;
	private StatusWrapper _statusWrapper;
	private Status _status;
	private Comment _comment;
	private User _userInfo;
	private Remind _reminds;
	private Favorite _favorite;
	private UserWrapper _userWrapper;
	private CommentWrapper _commentWrapper;
	private RepostWrapper _repostWrapper;
	private FavoriteWrapper _favoriteWrapper;
	private TagsWrapper _tagsWrapper;
	private byte[] _imageData;

	public RefreshContentsScreen(WeiboSDK weibo, String param, int action)
	{
		super(new VerticalFieldManager());

		_weibo = weibo;
		_param = param;

		add(new LabelField("Please wait....", Field.FIELD_HCENTER));

		switch (action)
		{
			//case RefreshAction.MYSELFTIMELINE:
			//	thread = new Thread(new MyTimelineRunnable());
			//	break;
			case RefreshAction.SENDTEXTSTATUS:
				thread = new Thread(new SentTextStatusRunnable());
				break;
			case RefreshAction.REPOSTTIMELINE:
				thread = new Thread(new RepostTimelineRunnable());
				break;
			case RefreshAction.USERAVATARHD:
				thread = new Thread(new UserAvatarHdRunnable());
				break;
			case RefreshAction.DESTROYFRIENDSHIP:
			case RefreshAction.DESTROYFOLLOWER:
				thread = new Thread(new DestroyFriendshipRunnable(action));
				break;
			case RefreshAction.CREATEFRIENDSHIP:
				thread = new Thread(new CreateFriendshipRunnable());
				break;
		}

		thread.start();
	}

	public RefreshContentsScreen(WeiboSDK weibo, final Hashtable params, final int action)
	{
		super(new VerticalFieldManager());

		_weibo = weibo;

		add(new LabelField("Please wait....", Field.FIELD_HCENTER));

		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run()
			{
				switch (action)
				{
					case RefreshAction.PUBLICTIMELINE:
						thread = new Thread(new PublicTimelineRunnable(params));
						break;
					case RefreshAction.USERTIMELINE:
						thread = new Thread(new UserTimelineRunnable(params));
						break;
					case RefreshAction.STATUSMENTIONS:
						thread = new Thread(new StatusMentionsRunnable(params));
						break;
					case RefreshAction.COMMENTMENTIONS:
						thread = new Thread(new CommentMentionsRunnable(params));
						break;
					case RefreshAction.COMMENTTOME:
						thread = new Thread(new CommentToMeRunnable(params));
						break;
					case RefreshAction.COMMENTBYME:
						thread = new Thread(new CommentByMeRunnable(params));
						break;
					case RefreshAction.FRIENDSTIMELINE:
						thread = new Thread(new FriendsTimelilneRunnable(params));
						break;
					case RefreshAction.GROUPTIMELINE:
						thread = new Thread(new GroupTimelilneRunnable(params));
						break;
					case RefreshAction.REPOSTSTATUS:
						thread = new Thread(new RepostStatusRunnable(params));
						break;
					case RefreshAction.REPLYSTATUS:
						thread = new Thread(new ReplyStatusRunnable(params));
						break;
					case RefreshAction.REPLYCOMMENT:
						thread = new Thread(new ReplyCommentRunnable(params));
						break;
					case RefreshAction.USERINFO:
						thread = new Thread(new UserInfoRunnable(params));
						break;
					case RefreshAction.REFRESHREMIND:
						thread = new Thread(new RefreshRemindRunnable());
						break;
					case RefreshAction.USERLISTFRIENDS:
					case RefreshAction.USERLISTFOLLOWERS:
						thread = new Thread(new UserListRunnable(action, params));
						break;
					case RefreshAction.SENDIMAGESTATUS:
						thread = new Thread(new SentImageStatusRunnable(params));
						break;
					case RefreshAction.FAVORITES:
						thread = new Thread(new FavoritesRunnable(params));
						break;
					case RefreshAction.FAVORITESTAGS:
						thread = new Thread(new FavoriteTagsRunnable(params));
						break;
					case RefreshAction.FAVORITESBYTAGS:
						thread = new Thread(new FavoriteByTagsTagsRunnable(params));
						break;
					case RefreshAction.FAVORITESDESTROY:
						thread = new Thread(new FavoriteDestroyRunnable(params));
						break;
					case RefreshAction.FAVORITESCREATE:
						thread = new Thread(new FavoriteCreateRunnable(params));
						break;
				}

				thread.start();				
			}
		});
	}

	class UserAvatarHdRunnable implements Runnable
	{
		public void run()
		{
			_imageData = _weibo.doRequestRAW(_param);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class DestroyFriendshipRunnable implements Runnable
	{
		int _action = 0;

		public DestroyFriendshipRunnable(int action) {_action = action;}

		public void run()
		{
			switch (_action)
			{
				case RefreshAction.DESTROYFOLLOWER:
					_userInfo = _weibo.DestroyFollower(_param);
					break;
				case RefreshAction.DESTROYFRIENDSHIP:
					_userInfo = _weibo.DestroyFriendship(_param);
			}

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class CreateFriendshipRunnable implements Runnable
	{
		public void run()
		{
			_userInfo = _weibo.CreateFriendship(_param);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class PublicTimelineRunnable implements Runnable
	{
		String count = "";

		public PublicTimelineRunnable(Hashtable params)
		{
			count = (String) params.get("count");
		}

		public void run()
		{
			_statusWrapper = _weibo.getPublicTimeline(count);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class FriendsTimelilneRunnable implements Runnable
	{
		String count = "";
		String page = "";
		String max_id = "";

		public FriendsTimelilneRunnable(Hashtable params)
		{
			count = (String) params.get("count");
			page = (String) params.get("page");
			max_id = (String) params.get("max_id");
		}

		public void run()
		{
			_statusWrapper = _weibo.getFriendsTimeline(count, page, max_id);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class GroupTimelilneRunnable implements Runnable
	{
		String list_id = "";
		String count = "";
		String page = "";
		String max_id = "";

		public GroupTimelilneRunnable(Hashtable params)
		{
			list_id = (String) params.get("list_id");
			count = (String) params.get("count");
			page = (String) params.get("page");
			max_id = (String) params.get("max_id");
		}

		public void run()
		{
			_statusWrapper = _weibo.getGroupsTimeline(list_id, count, page, max_id);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class FavoritesRunnable implements Runnable
	{
		String count = "";
		String page = "";

		public FavoritesRunnable(Hashtable params)
		{
			count = (String) params.get("count");
			page = (String) params.get("page");
		}

		public void run()
		{
			_favoriteWrapper = _weibo.getFavorites(count, page);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class FavoriteTagsRunnable implements Runnable
	{
		String count = "";
		String page = "";

		public FavoriteTagsRunnable(Hashtable params)
		{
			count = (String) params.get("count");
			page = (String) params.get("page");
		}

		public void run()
		{
			_tagsWrapper = _weibo.getFavoriteTags(count, page);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class FavoriteByTagsTagsRunnable implements Runnable
	{
		String tid = "";
		String count = "";
		String page = "";

		public FavoriteByTagsTagsRunnable(Hashtable params)
		{
			tid = (String) params.get("tid");
			count = (String) params.get("count");
			page = (String) params.get("page");
		}

		public void run()
		{
			_favoriteWrapper = _weibo.getFavoritesByTags(tid, count, page);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class FavoriteDestroyRunnable implements Runnable
	{
		String id = "";

		public FavoriteDestroyRunnable(Hashtable params) {id = (String) params.get("id");}

		public void run()
		{
			_favorite = _weibo.DestroyFavorites(id);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class FavoriteCreateRunnable implements Runnable
	{
		String id = "";

		public FavoriteCreateRunnable(Hashtable params) {id = (String) params.get("id");}

		public void run()
		{
			_favorite = _weibo.CreateFavorites(id);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	//class MyTimelineRunnable implements Runnable
	//{
	//	public void run()
	//	{
	//		_statusWrapper = _weibo.getMyTimeline();

	//		Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
	//	}
	//}

	class UserTimelineRunnable implements Runnable
	{
		String uid = "";
		String count = "";
		String page = "";
		String max_id = "";

		public UserTimelineRunnable(Hashtable params)
		{
			uid = (String) params.get("uid");
			count = (String) params.get("count");
			page = (String) params.get("page");
			max_id = (String) params.get("max_id");
		}

		public void run()
		{
			_statusWrapper = _weibo.getUserTimeline(uid, count ,page, max_id);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class UserInfoRunnable implements Runnable
	{
		private long uid = 0;
		private String screen_name = "";

		public UserInfoRunnable(Hashtable params)
		{
			uid = Long.parseLong((String) params.get("uid"));
			screen_name = (String) params.get("screen_name");
		}

		public void run()
		{
			_userInfo = _weibo.getUserInfo(uid, screen_name);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class RefreshRemindRunnable implements Runnable
	{
		public void run()
		{
			_reminds = _weibo.getRemind();

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class UserListRunnable implements Runnable
	{
		private int type = 0;
		private String uid = "";
		private String count = "";
		private String cursor = "";

		public UserListRunnable(int type, Hashtable params)
		{
			uid = (String) params.get("uid");
			count = (String) params.get("count");
			cursor = (String) params.get("cursor");
			this.type = type;
		}

		public void run()
		{
			_userWrapper = _weibo.getUserList(type, uid, count, cursor);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class StatusMentionsRunnable implements Runnable
	{
		String count = "";
		String page = "";
		String max_id = "";

		public StatusMentionsRunnable(Hashtable params)
		{
			count = (String) params.get("count");
			page = (String) params.get("page");
			max_id = (String) params.get("max_id");
		}

		public void run()
		{
			_statusWrapper = _weibo.getStatusMentions(count, page, max_id);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class CommentMentionsRunnable implements Runnable
	{
		String count = "";
		String page = "";
		String max_id = "";

		public CommentMentionsRunnable(Hashtable params)
		{
			count = (String) params.get("count");
			page = (String) params.get("page");
			max_id = (String) params.get("max_id");
		}

		public void run()
		{
			_commentWrapper = _weibo.getCommentMentions(count, page, max_id);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class CommentToMeRunnable implements Runnable
	{
		String count = "";
		String page = "";
		String max_id = "";

		public CommentToMeRunnable(Hashtable params)
		{
			count = (String) params.get("count");
			page = (String) params.get("page");
			max_id = (String) params.get("max_id");
		}

		public void run()
		{
			_commentWrapper = _weibo.getCommentToMe(count, page, max_id);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class CommentByMeRunnable implements Runnable
	{
		String count = "";
		String page = "";
		String max_id = "";

		public CommentByMeRunnable(Hashtable params)
		{
			count = (String) params.get("count");
			page = (String) params.get("page");
			max_id = (String) params.get("max_id");
		}

		public void run()
		{
			_commentWrapper = _weibo.getCommentByMe(count, page, max_id);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class SentImageStatusRunnable implements Runnable
	{
		String status = "";
		ImageItem[] images;

		public SentImageStatusRunnable(Hashtable params)
		{
			status = (String) params.get("status");
			images = (ImageItem[]) params.get("images");
		}

		public void run()
		{
			_status = _weibo.uploadStatus(status, images);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class SentTextStatusRunnable implements Runnable
	{
		public void run()
		{
			_status = _weibo.updateStatus(_param);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class RepostStatusRunnable implements Runnable
	{
		String id = "";
		String status = "";
		String is_comment = "";

		public RepostStatusRunnable(Hashtable params)
		{
			id = (String) params.get("id");
			status = (String) params.get("status");
			is_comment = (String) params.get("is_comment");
		}

		public void run()
		{
			_status = _weibo.repostStatus(id, status, is_comment);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class ReplyStatusRunnable implements Runnable
	{
		String id = "";
		String comment = "";
		String comment_ori = "";

		public ReplyStatusRunnable(Hashtable params)
		{
			id = (String) params.get("id");
			comment = (String) params.get("comment");
			comment_ori = (String) params.get("comment_ori");
		}

		public void run()
		{
			_comment = _weibo.replyStatus(id, comment, comment_ori);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class ReplyCommentRunnable implements Runnable
	{
		String id = "";
		String cid = "";
		String comment = "";
		//String without_mention = "";
		String comment_ori = "";

		public ReplyCommentRunnable(Hashtable params)
		{
			id = (String) params.get("id");
			cid = (String) params.get("cid");
			comment = (String) params.get("comment");
			//without_mention = (String) params.get("without_mention");
			comment_ori = (String) params.get("comment_ori");
		}

		public void run()
		{
			_comment = _weibo.replyComment(id, cid, comment, comment_ori);

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	class RepostTimelineRunnable implements Runnable
	{
		public void run()
		{
			_repostWrapper = _weibo.getRepostTimeline(Long.parseLong(_param));

			Application.getApplication().invokeLater(new Runnable() {public void run() {onClose();}});
		}
	}

	public StatusWrapper getStatusWrapper() {return _statusWrapper;}

	public CommentWrapper getCommentWrapper() {return _commentWrapper;}

	public User getUserInfo() {return _userInfo;}

	public Remind getReminds() {return _reminds;}

	public UserWrapper getUserWrapper() {return _userWrapper;}

	public RepostWrapper getRepostWrapper() {return _repostWrapper;}

	public Favorite getFavorite() {return _favorite;}

	public FavoriteWrapper getFavoriteWrapper() {return _favoriteWrapper;}

	public TagsWrapper getTagsWrapper() {return _tagsWrapper;}

	public Status getStatus() {return _status;}

	public Comment getComment() {return _comment;}

	public byte[] getImageData() {return _imageData;}

	public boolean onClose()
	{
		if (thread != null)
		{
			try {
				thread.interrupt();
				thread = null;
			} catch (Exception e) {}
		}

		try {
			UiApplication.getUiApplication().popScreen(this);	
		} catch (Exception e) {}

		return true;
	}

	protected boolean keyChar(char key, int status, int time)
	{
		if (key == Characters.ESCAPE)
		{
			onClose();

			return true;
		}

		return super.keyChar(key, status, time);
	}
} 